dojo.provide("miaigo.model.sgf.PropertyFactory");

dojo.require("miaigo.model.sgf.SGFProperty");
dojo.require("miaigo.model.sgf.values.ColorValueHandler");
dojo.require("miaigo.model.sgf.values.IntegerValueHandler");
dojo.require("miaigo.model.sgf.values.LabelValueHandler");
dojo.require("miaigo.model.sgf.values.LineValueHandler");
dojo.require("miaigo.model.sgf.values.ListValueHandler");
dojo.require("miaigo.model.sgf.values.NoTypeValueHandler");
dojo.require("miaigo.model.sgf.values.PointValueHandler");
dojo.require("miaigo.model.sgf.values.RealValueHandler");
dojo.require("miaigo.model.sgf.values.TextValueHandler");

miaigo.model.sgf.PropertyFactory.propertyDefs =
    {
	"B" : { type: "move",  valueType: "Point",   attrName: "blackMove" },
	"BL": { type: "move",  valueType: "Real",    attrName: "blackTimeLeft" },
	"BM": { type: "move",  valueType: "Integer", attrName: "badMove",
		minValue: 1, maxValue: 2 },
	"DO": { type: "move",  valueType: "NoType",  attrName: "doubtfulMove",
		inherit: false },
	"IT": { type: "move",  valueType: "NoType",  attrName: "interestingMove",
		inherit: false },
	"KO": { type: "move",  valueType: "NoType",  attrName: "forceMove",
		inherit: false },
	"MN": { type: "move",  valueType: "Integer", attrName: "moveNumber" },
	"OB": { type: "move",  valueType: "Integer", attrName: "blackMoveLeft" },
	"OW": { type: "move",  valueType: "Integer", attrName: "whiteMoveLeft" },
	"TE": { type: "move",  valueType: "Integer", attrName: "tesuji",
		minValue: 1, maxValue: 2 },
	"W" : { type: "move",  valueType: "Point",   attrName: "whiteMove" },
	"WL": { type: "move",  valueType: "Real",    attrName: "whiteTimeLeft" },
	"AB": { type: "setup", valueType: "List",    attrName: "addBlack",
		itemType: "Point" },
	"AE": { type: "setup", valueType: "List",    attrName: "addEmpty",
		itemType: "Point" },
	"AW": { type: "setup", valueType: "List",    attrName: "addWhite",
		itemType: "Point" },
	"PL": { type: "setup", valueType: "Color",   attrName: "setPlayer" },
	"AR": { type: "none",  valueType: "List",    attrName: "arrow",
		itemType: "Line" },
	"C" : { type: "none",  valueType: "Text",    attrName: "comment" },
	"CR": { type: "none",  valueType: "List",    attrName: "circle",
		itemType: "Point" },
	"DD": { type: "none",  valueType: "List",    attrName: "dimPoints",
		itemType: "Point", inherit: true },
	"DM": { type: "none",  valueType: "Integer", attrName: "even",
		minValue: 1, maxValue: 2 },
	"GB": { type: "none",  valueType: "Integer", attrName: "goodForBlack",
		minValue: 1, maxValue: 2 },
	"GW": { type: "none",  valueType: "Integer", attrName: "goodForWhite",
		minValue: 1, maxValue: 2 },
	"HO": { type: "none",  valueType: "Integer", attrName: "hotspot",
		minValue: 1, maxValue: 2 },
	"LB": { type: "none",  valueType: "List",    attrName: "label",
		itemType: "Label" },
	"LN": { type: "none",  valueType: "List",    attrName: "line",
		itemType: "Line" },
	"MA": { type: "none",  valueType: "List",    attrName: "mark",
		itemType: "Point" },
	"N" : { type: "none",  valueType: "Text",    attrName: "nodeName",
		isSimple: true},
	"PM": { type: "none",  valueType: "Integer", attrName: "printMoveMode",
		inherit: true },
	"SL": { type: "none",  valueType: "List",    attrName: "selected",
		itemType: "Point" },
	"SQ": { type: "none",  valueType: "List",    attrName: "square",
		itemType: "Point" },
	"TR": { type: "none",  valueType: "List",    attrName: "triangle",
		itemType: "Point" },
	"UC": { type: "none",  valueType: "Integer", attrName: "unclear",
		minValue: 1, maxValue: 2 },
	"V" : { type: "none",  valueType: "Real",    attrName: "value" },
	"VW": { type: "none",  valueType: "List",    attrName: "view",
		itemType: "Point" },
	"TB": { type: "none",  valueType: "List",    attrName: "blackTerritory",
		itemType: "Point" },
	"TW": { type: "none",  valueType: "List",    attrName: "whiteTerritory",
		itemType: "Point" },
	"AP": { type: "root",  valueType: "Text",    attrName: "application",
		isSimple: true },
	"CA": { type: "root",  valueType: "Text",    attrName: "charset",
		isSimple: true },
	"FF": { type: "root",  valueType: "Integer", attrName: "fileFormat",
		minValue: 1, maxValue: 4 },
	"GM": { type: "root",  valueType: "Integer", attrName: "gameType",
		minValue: 1, maxValue: 1 },
	"ST": { type: "root",  valueType: "Integer", attrName: "gameStyle",
		minValue: 0, maxValue: 3 },
	"SZ": { type: "root",  valueType: "Integer", attrName: "boardSize" },
	"AN": { type: "info",  valueType: "Text",    attrName: "annotation",
		isSimple: true },
	"BR": { type: "info",  valueType: "Text",    attrName: "blackRank",
		isSimple: true },
	"BT": { type: "info",  valueType: "Text",    attrName: "blackTeam",
		isSimple: true },
	"CP": { type: "info",  valueType: "Text",    attrName: "copyright",
		isSimple: true },
	"DT": { type: "info",  valueType: "Text",    attrName: "date",
		isSimple: true },
	"EV": { type: "info",  valueType: "Text",    attrName: "event",
		isSimple: true },
	"GC": { type: "info",  valueType: "Text",    attrName: "gameComment" },
	"GN": { type: "info",  valueType: "Text",    attrName: "gameName",
		isSimple: true },
	"ON": { type: "info",  valueType: "Text",    attrName: "opening",
		isSimple: true },
	"OT": { type: "info",  valueType: "Text",    attrName: "overtime",
		isSimple: true },
	"PB": { type: "info",  valueType: "Text",    attrName: "blackPlayer",
		isSimple: true },
	"PC": { type: "info",  valueType: "Text",    attrName: "place",
		isSimple: true },
	"PW": { type: "info",  valueType: "Text",    attrName: "whitePlayer",
		isSimple: true },
	"RE": { type: "info",  valueType: "Text",    attrName: "result",
		isSimple: true },
	"RO": { type: "info",  valueType: "Text",    attrName: "round",
		isSimple: true },
	"RU": { type: "info",  valueType: "Text",    attrName: "rules",
		isSimple: true },
	"SO": { type: "info",  valueType: "Text",    attrName: "source",
		isSimple: true },
	"TM": { type: "info",  valueType: "Real",    attrName: "timeLimit" },
	"US": { type: "info",  valueType: "Text",    attrName: "user",
		isSimple: true },
	"WR": { type: "info",  valueType: "Text",    attrName: "whiteRank",
		isSimple: true },
	"WT": { type: "info",  valueType: "Text",    attrName: "whiteTeam",
		isSimple: true },
	"HA": { type: "info",  valueType: "Integer", attrName: "handicap",
		inherit: false },
	"KM": { type: "info",  valueType: "Text",    attrName: "komi",
		isSimple: true }
	// Not Implemented Yet:
	// "FG": { type: "none",  valueType: "none | composed number ":" simpletext", inherit: false }
    };

miaigo.model.sgf.PropertyFactory.createProperty = function(/*String*/propID)
{
    var prop = null;
    var propDef = miaigo.model.sgf.PropertyFactory.propertyDefs[propID];
    var valueHandler = null;

    // create value handler
    if (propDef)
    {
	var valueTypeClass = "miaigo.model.sgf.values." + propDef.valueType + "ValueHandler";
	var valueTypeClassObject = dojo.getObject(valueTypeClass);
	if (valueTypeClassObject)
	{
	    valueHandler = new valueTypeClassObject(propDef);
	}
    }
    else
    {
	throw "Cannot find property definition for " + propID;
    }

    // create proerty

    if (valueHandler)
    {
	var prop = new miaigo.model.sgf.SGFProperty(propID, propDef.type, valueHandler);
    }
    else
    {
	throw "Cannot create value handler for " + propID;
    }

    return prop;
};

miaigo.model.sgf.PropertyFactory.attributeName = function(/*String*/propID)
{
    var propDef = miaigo.model.sgf.PropertyFactory.propertyDefs[propID];
    if (propDef)
    {
	return propDef.attrName;
    }

    return null;
};